const app = getApp()
const db = wx.cloud.database()
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    hidden: true,
    contactAll:[],
    contactCur:[],
    concatData:[],
    issearch: false,
    selectid: 0,
    selectsubid: 0
  },

  onLoad: function(options) {
    wx.showLoading({
      title: '数据加载中...',
    })
    let list = []
    var contactAll = []
    for (let i = 0; i < 26; i++) {
      list[i] = String.fromCharCode(65 + i)
      contactAll[i] = new Array()
    }
    const MAX_LIMIT = 20
    db.collection('contact').count().then(res => {
      const total = res.total
      const batchTimes = Math.ceil(total / 20)
      const tasks = []
      for (let i = 0; i < batchTimes; i++) {
        const promise = db.collection('contact').skip(i * MAX_LIMIT).limit(MAX_LIMIT).get();
        tasks.push(promise)
      }

      Promise.all(tasks).then((result) => {
        let concatData = []
        for (let i = 0; i < result.length; i++) {
          concatData = concatData.concat(result[i].data)
        }
        for (let i = 0; i < concatData.length; i++) {
          let index = concatData[i].searchfeild[0].charCodeAt() - 97
          contactAll[index].push(concatData[i])
        }
        this.setData({
          list: list,
          listCur: list[0],
          contactAll: contactAll,
          contactCur: contactAll,
          concatData: concatData
        })
        wx.hideLoading()
      })
    })
  },

  bindKeyInput: function (event) {
    var value = event.detail.value
    value = value.toLowerCase()
    var contactAll=this.data.contactAll
    var concatData=this.data.concatData
    var contactCur=[]
    if (event.detail.value == '') {
      this.setData({
        issearch: false,
        contactCur: contactAll
      })
    }
    else
    {
      for (let i = 0; i < concatData.length; i++) {
        let index = concatData[i].searchfeild.indexOf(value)
        if(index!=-1)
          contactCur.push(concatData[i])
      }
      console.log(contactCur)
      this.setData({
        issearch:true,
        contactCur: contactCur
      })
    }
  },

  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target,
      selectid: e.currentTarget.id,
      selectsubid: e.currentTarget.dataset.id
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },

  makephone: function (event) {
    var phoneNumber
    if (!this.data.issearch) {
      phoneNumber = this.data.contactCur[this.data.selectid][this.data.selectsubid].mobile
    } else {
      phoneNumber = this.data.contactCur[this.data.selectsubid].mobile
    }
    //console.log(phoneNumber)
    wx.makePhoneCall({
      phoneNumber: phoneNumber.toString() //仅为示例，并非真实的电话号码
    })
  },

  addcontact: function (event) {
    var userinfo
    if (!this.data.issearch) {
      userinfo = this.data.contactCur[this.data.selectid][this.data.selectsubid]
    } else {
      userinfo = this.data.contactCur[this.data.selectsubid]
    }
    wx.addPhoneContact({
      firstName: userinfo.name,
      mobilePhoneNumber: userinfo.mobile.toString(),
      success: function () {
        console.log('添加成功')
      }
    })
  },

  onReady() {
    let that = this;
    wx.createSelectorQuery().select('.indexBar-box').boundingClientRect(function (res) {
      that.setData({
        boxTop: res.top
      })
    }).exec();
    wx.createSelectorQuery().select('.indexes').boundingClientRect(function (res) {
      that.setData({
        barTop: res.top
      })
    }).exec()
  },
  //获取文字信息
  getCur(e) {
    this.setData({
      hidden: false,
      listCur: this.data.list[e.target.id],
    })
  },

  setCur(e) {
    this.setData({
      hidden: true,
      listCur: this.data.listCur
    })
  },
  //滑动选择Item
  tMove(e) {
    let y = e.touches[0].clientY,
      offsettop = this.data.boxTop,
      that = this;
    //判断选择区域,只有在选择区才会生效
    if (y > offsettop) {
      let num = parseInt((y - offsettop) / 20);
      this.setData({
        listCur: that.data.list[num]
      })
    };
  },

  //触发全部开始选择
  tStart() {
    this.setData({
      hidden: false
    })
  },

  //触发结束选择
  tEnd() {
    this.setData({
      hidden: true,
      listCurID: this.data.listCur
    })
  },
  indexSelect(e) {
    let that = this;
    let barHeight = this.data.barHeight;
    let list = this.data.list;
    let scrollY = Math.ceil(list.length * e.detail.y / barHeight);
    for (let i = 0; i < list.length; i++) {
      if (scrollY < i + 1) {
        that.setData({
          listCur: list[i],
          movableY: i * 20
        })
        return false
      }
    }
  }
})